package com.epalmpay.controller.channel.mall;

import java.util.List;


import com.epalmpay.commom.BaseController;
import com.epalmpay.entity.MallCategory;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.mall.CategoryOutDTO;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.dto.mall.MallCategoryInDTO;
import com.epalmpay.dto.mall.MallCategoryOutDTO;
import com.epalmpay.dto.mall.MallCategoryQueryDTO;
import com.epalmpay.service.mall.impl.MallCategoryServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * Created by lifayi on 2017/5/15.
 */
@RestController
@RequestMapping("/merchant/mall")
public class MallCategoryController  extends BaseController{

    @Autowired
    private MallCategoryServiceImpl mallCategoryService;

    /*
    * 获取商品类目列表
    * */
    @RequestMapping("/mallCategory")
    public List<MallCategoryOutDTO> mallCategory(@RequestBody MallCategoryQueryDTO MallCategoryQueryDTO) {
        try {
            return mallCategoryService.mallCategory(MallCategoryQueryDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /*
    * 添加商品类目
    * */
    @RequestMapping("/mallCategoryAdd")
    public MallCategoryOutDTO mallCategoryAdd(@RequestBody MallCategoryInDTO mallCategoryInDTO) {
        try {
            return mallCategoryService.mallCategoryAdd(mallCategoryInDTO);
        } catch (Exception e) {
            e.printStackTrace();
            MallCategoryOutDTO mallCategoryOutDTO = new MallCategoryOutDTO();
            mallCategoryOutDTO.setErrorCode(Rescode.FAIL);
            return mallCategoryOutDTO;
        }
    }

    /**
     * 异步校验类目名称的唯一性
     * @param mallCategoryInDTO
     * @return
     */
    @RequestMapping("/checkCategoryName")
    public MallCategoryOutDTO selectAndCheckCategoryName(@RequestBody MallCategoryInDTO mallCategoryInDTO){
        MallCategoryOutDTO mallCategoryOutDTO = new MallCategoryOutDTO();
        try {
             mallCategoryOutDTO=mallCategoryService.selectAndCheckCategoryName(mallCategoryInDTO);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("类目名称校验"+e);
            mallCategoryOutDTO.setErrorCode("-2","类目校验，系统异常");
        }
        return mallCategoryOutDTO;
    }

    /**
     * 修改类目(名称)
     * @param mallCategoryInDTO
     * @return
     */
    @RequestMapping("/saveEditCategory")
    public MallCategoryOutDTO updateEditCategory(@RequestBody MallCategoryInDTO mallCategoryInDTO){
        int sum=-1;
        MallCategoryOutDTO mallCategoryOutDTO = new MallCategoryOutDTO();
        try {
            sum=mallCategoryService.updateCategoryById(mallCategoryInDTO);
            if (sum>=1){
                mallCategoryOutDTO.setErrorCode("1","修改成功");
            }else{
                mallCategoryOutDTO.setErrorCode("-1","修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("类目名称修改"+e);
            mallCategoryOutDTO.setErrorCode("-2","类目名称修改，系统异常");
        }
        return mallCategoryOutDTO;
    }
    /*
    * 商品类目移动
    * */
    @RequestMapping("/mallCategoryResort")
    public BaseOutDTO mallCategoryResort(@RequestBody List<MallCategoryInDTO> mallCategoryInDTOList) {
        try {
            return mallCategoryService.mallCategoryResort(mallCategoryInDTOList);
        } catch (Exception e) {
            e.printStackTrace();
            BaseOutDTO baseOutDTO = new BaseOutDTO();
            baseOutDTO.setErrorCode(Rescode.FAIL);
            return baseOutDTO;
        }
    }

    /*
    * 商品类目删除
    * */
    @RequestMapping("/mallCategoryDel")
    public BaseOutDTO mallCategoryDel(@RequestBody MallCategoryInDTO mallCategoryInDTO) {
        return mallCategoryService.mallCategoryDel(mallCategoryInDTO);
    }

    /*
    * 商品类目图标上传
    * */
    @RequestMapping("/mallCategoryIcon")
    public BaseOutDTO mallCategoryIcon(@RequestBody MallCategoryInDTO mallCategoryInDTO) {
        return mallCategoryService.mallCategoryIcon(mallCategoryInDTO);
    }

    /*
    * 根据parentID值获取子类目，如果为null表示获取一级类目
    * */
    @RequestMapping(value = "/getMallCategoryNameAndID", method = RequestMethod.POST)
    public List<MallCategoryOutDTO> getMallCategoryNameAndID(@RequestBody MallCategoryQueryDTO mallCategoryQueryDTO) {
        try {
            return mallCategoryService.getMallCategoryNameAndID(mallCategoryQueryDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping(value = "/getMallCategoryNameAndID", method = RequestMethod.GET)
    public List<MallCategoryOutDTO> getMallCategoryNameAndID1(@RequestParam Byte goodBigtype,@RequestParam Long groupId,Long parentId) {
        try {
            MallCategoryQueryDTO mallCategoryQueryDTO = new MallCategoryQueryDTO();
            mallCategoryQueryDTO.setGoodBigtype(goodBigtype);
            mallCategoryQueryDTO.setParentId(parentId);
            mallCategoryQueryDTO.setGroupId(groupId);
            return mallCategoryService.getMallCategoryNameAndID(mallCategoryQueryDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping(value = "/getMallCategoryNameAndID2", method = RequestMethod.GET)
    public List<MallCategory> getMallCategoryNameAndID2(@RequestParam Byte goodBigtype, @RequestParam Long groupId) {
        try {
            MallCategoryQueryDTO mallCategoryQueryDTO = new MallCategoryQueryDTO();
            mallCategoryQueryDTO.setGoodBigtype(goodBigtype);
            mallCategoryQueryDTO.setParentId(null);
            mallCategoryQueryDTO.setGroupId(groupId);
            return mallCategoryService.getMallCategoryNameAndID2(mallCategoryQueryDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /*
    * 获取一级类目并且根据parentID的值（默认是一级类目中的第一个）获取二级类目
    * */
    @RequestMapping("/getCategory")
    public CategoryOutDTO getCategory(@RequestBody MallCategoryQueryDTO mallCategoryQueryDTO) {
        return mallCategoryService.getCategory(mallCategoryQueryDTO);
    }


    @RequestMapping("/getShareMallCategoryNameAndID")
    public List<MallCategoryOutDTO> getShareMallCategoryNameAndID(@RequestBody MallCategoryQueryDTO mallCategoryQueryDTO) {
        try {
            return mallCategoryService.getShareMallCategoryNameAndID(mallCategoryQueryDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
